package com.hdw.med.conf;

import com.hdw.common.db.DynamicDataSource;
import com.hdw.common.db.HikariDataSourceExt;
import com.hdw.common.util.DBUtil;
import com.hdw.med.api.BaseDbService;
import com.hdw.med.bean.model.BaseDb;
import com.zaxxer.hikari.HikariDataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.util.List;

@Component
public class BaseDBComponent implements InitializingBean {
    private static Logger logger = LoggerFactory.getLogger(BaseDBComponent.class);
    @Autowired
    private BaseDbService baseDbService;
    @Autowired
    private DynamicDataSource dynamicDataSource;

    public static HikariDataSourceExt createDataSource(BaseDb baseDb) {
        HikariDataSourceExt datasource = DataSourceBuilder.create()
                .type(HikariDataSourceExt.class)
                .driverClassName(baseDb.getDriver())
                .url(baseDb.getUrl())
                .username(baseDb.getUsename())
                .password(baseDb.getPsword())
                .build();
        datasource.setIdDb(baseDb.getIdDb().toString());
        datasource.setCharacterSet(baseDb.getCharacterSet());
        datasource.setEuTp(baseDb.getEuTp());
        datasource.setEuDbTp(baseDb.getEuDbTp());
        return datasource;
    }


    public void removeDataSource(String dbId) {
        dynamicDataSource.removeDataSource(dbId);
        logger.info(String.format("----------> DynamicDataSource remove [%s].", dbId));
    }

    public void addDataSource(BaseDb baseDb) {
        HikariDataSourceExt dataSource = createDataSource(baseDb);
        Connection connection = null;
        boolean success = false;

        try {
            connection = dataSource.getConnection();

            success = true;


            if ("2".equals(baseDb.getEuDbTp())) {
                logger.info(String.format("----------> DynamicDataSource add [%s] [%s] [%s] %s.", DBUtil.DB_DW, dataSource.getIdDb(), baseDb.getNa(), success ? "success" : "fail"));
                dynamicDataSource.addDataSource(DBUtil.DB_DW, dataSource);
            }
            dynamicDataSource.addDataSource(dataSource.getIdDb(), dataSource);
        } catch (Exception e) {
        } finally {
            DBUtil.closeDBResources(null, null, connection);
        }
        logger.info(String.format("----------> DynamicDataSource add [%s] [%s] [%s] %s.", dataSource.getIdDb(), dataSource.getIdDb(), baseDb.getNa(), success ? "success" : "fail"));
    }


    @Override
    public void afterPropertiesSet() throws Exception {
        BaseDb baseDb1 = new BaseDb();
        baseDb1.setFgStop("0");
        List<BaseDb> pageVO = baseDbService.findByModel(baseDb1);
        for (BaseDb baseDb : pageVO) {
            addDataSource(baseDb);
        }
    }


}
